package com.example.filecabinet.service.impl;

import com.example.filecabinet.constant.RecordConstant;
import com.example.filecabinet.dao.Lattice_infoMapper;
import com.example.filecabinet.entity.Lattice_info;
import com.example.filecabinet.entity.Lattice_infoExample;
import com.example.filecabinet.service.Lattice_infoService;
import com.example.filecabinet.util.AutoIdFactory;
import com.thfdcsoft.framework.common.date.DateFormatConstant;
import com.thfdcsoft.framework.common.date.DateFormatFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author chengconghui
 * @Description //TODO
 * @Date 10:23 2020/3/23
 **/
@Service
public class Lattice_infoServiceImpl implements Lattice_infoService {

    @Autowired
    Lattice_infoMapper lattice_infoMapper;

    @Override
    public List<Lattice_info> selectByExample(Lattice_infoExample example) {
        return lattice_infoMapper.selectByExample(example);
    }

    @Override
    public int updateByPrimaryKeySelective(Lattice_info record) {
        return lattice_infoMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public List<Lattice_info> selectEmpTakeLattice(String employeeId, Lattice_info lattice_info) {
        return lattice_infoMapper.selectEmpTakeLattice(employeeId, lattice_info);
    }

    @Override
    public List<Lattice_info> selectByDate(String deviceNumber) {
        return lattice_infoMapper.selectByDate(deviceNumber);
    }

    @Override
    public int insertSelective(Lattice_info record) {
        return lattice_infoMapper.insertSelective(record);
    }

    /**
     * 添加单个格子
     *
     * @param i
     * @param deviceNumber
     */
    @Override
    public void addLattice(int i, String deviceNumber) {
        Lattice_info lattice_info = new Lattice_info();
        lattice_info.setLatticeId(AutoIdFactory.getAutoId());
        lattice_info.setLatticeNumber(String.valueOf(i));
        lattice_info.setDeviceNumber(deviceNumber);
        lattice_info.setLatticeLdleStatus(RecordConstant.LATTIC_LDLE_STATUS.EMPTY);
        lattice_info.setLatticeSwithStatus(RecordConstant.LATTICE_SWITH_STATUS.CLOSE);
        lattice_info.setLatticeUpdateTime(DateFormatFactory.getCurrentDateString(DateFormatConstant.SimpleFormat.YYYY_MM_DD_HH_MM_SS_SSS));
        lattice_info.setLatticeOperator(RecordConstant.LATTICE_OPERATOR.STAFF);
        lattice_infoMapper.insertSelective(lattice_info);
    }

    /**
     * 用户判断查询某一段的格子是否在使用中
     *
     * @param oddCount
     * @param newCount
     * @param deviceNumber
     * @return
     */
    @Override
    public boolean selectLatticeUsing(int oddCount, int newCount, String deviceNumber) {
        Lattice_infoExample lattice_infoExample = new Lattice_infoExample();
        Lattice_infoExample.Criteria criteria = lattice_infoExample.createCriteria();
        criteria.andDeviceNumberEqualTo(deviceNumber);
        criteria.andLatticeLdleStatusEqualTo(RecordConstant.LATTIC_LDLE_STATUS.USING);
        criteria.andLatticeOperatorEqualTo(RecordConstant.LATTICE_OPERATOR.CUSTOMER);
        criteria.andLatticeNumberBetween(String.valueOf(newCount), String.valueOf(oddCount));
        List<Lattice_info> lattice_infos = lattice_infoMapper.selectByExample(lattice_infoExample);
        if (lattice_infos.size() != 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public int deleteByExample(int oddCount, int newCount, String deviceNumber) {
        int count = 0;
        for (int i = oddCount; i > newCount; i--) {
            Lattice_infoExample lattice_infoExample = new Lattice_infoExample();
            Lattice_infoExample.Criteria criteria = lattice_infoExample.createCriteria();
            criteria.andDeviceNumberEqualTo(deviceNumber);
            criteria.andLatticeNumberEqualTo(String.valueOf(i));
            count = count + lattice_infoMapper.deleteByExample(lattice_infoExample);
        }
        return count;
    }

    @Override
    public int restore(Lattice_info record) {
        record.setBusinessNumber("");
        record.setBusinessType("");
        record.setLatticeLdleStatus(RecordConstant.LATTIC_LDLE_STATUS.EMPTY);    //空闲状态00空闲
        record.setLatticeSwithStatus(RecordConstant.LATTICE_SWITH_STATUS.CLOSE);
        record.setLatticeRemarks1("");    //清空身份证号
        record.setLatticeOperator("");     //操作者 默认无
        int count = lattice_infoMapper.updateByPrimaryKeySelective(record);
        return count;
    }

    @Override
    public List<Lattice_info> selectOpenLattice(String deviceNumber) {
        return lattice_infoMapper.selectOpenLattice(deviceNumber);
    }

}
